package com.ruoyi.api.service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.api.dto.cleaning.resp.CleaningStatisticsRespDto;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.tools.utils.DateUtils;
import com.ruoyi.standingbook.cleaning.domain.Cleaning;
import com.ruoyi.standingbook.cleaning.domain.vo.CleaningVo;
import com.ruoyi.standingbook.cleaning.mapper.CleaningMapper;

import cn.hutool.core.util.StrUtil;
import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
@Service
public class CleaningStatService {
    private final CleaningMapper baseMapper;
    
	/**
	 * 
	 * @param date 管养时间
	 * @param pageQuery 页码信息
	 * 
	 * @return
	 */
    public TableDataInfo<CleaningStatisticsRespDto> queryCleaningList(String date, PageQuery pageQuery) {
		if (StrUtil.isBlank(date)) {
			date = DateUtils.getCurrDate();
		}
		
        LambdaQueryWrapper<Cleaning> lqw = Wrappers.lambdaQuery();
        lqw.eq(Cleaning::getDate, date);
        
        Page<CleaningVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw, CleaningVo.class);
        
        return TableDataInfo.build(fromCleaningVoToCleaningStat(result));
    }
    
    
    private Page<CleaningStatisticsRespDto> fromCleaningVoToCleaningStat(Page<CleaningVo> cleaningVoList) {
    	Page<CleaningStatisticsRespDto> page = new Page<>(); 
    	
    	page.setPages(page.getPages());
    	page.setTotal(page.getTotal());
    	
    	List<CleaningStatisticsRespDto> cleaningStatList = new ArrayList<>();
    	
    	SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
    	
    	cleaningVoList.getRecords().forEach(vo->{
    		CleaningStatisticsRespDto cleaningStatisticsRespDto = new CleaningStatisticsRespDto();
    		
    		BeanUtils.copyProperties(vo, cleaningStatisticsRespDto, "serviceType", "date", "pic1");
    		
    		cleaningStatisticsRespDto.setServiceType(vo.getServiceTypeName());
    		
    		cleaningStatisticsRespDto.setDate(formatter.format(vo.getDate()));
    		
    		cleaningStatisticsRespDto.setPic1Content(vo.getPic1());
    		
    		cleaningStatList.add(cleaningStatisticsRespDto);
    	});
    	
    	page.setRecords(cleaningStatList);
    	
    	return page;
    }
}
